package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FormulaPoJo;
import com.xbongbong.paas.field.NumericalLimitsPoJo;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.dto.FixDataJobDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.script.pojo.dto.AllScriptDTO;
import com.xbongbong.pro.script.pojo.dto.FieldSetDTO;
import com.xbongbong.pro.script.pojo.dto.FixFormDTO;
import com.xbongbong.pro.script.pojo.vo.ScriptVO;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.domain.entity.WorkOrderExplainEntity;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.enums.business.PurchaseEnum;
import com.xbongbong.saas.enums.business.RefundEnum;
import com.xbongbong.saas.enums.business.ReturnedPurchaseEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import com.xbongbong.saas.model.WorkOrderExplainModel;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.saas.script.tempenum.TempReturnedPurchaseEnum;
import com.xbongbong.saas.service.RepairExplainService;
import com.xbongbong.sys.model.CompanyModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author feng.zheng
 * @version v1.0
 * @date 2019/9/9 15:15
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/field")
public class FieldScriptController {
    private static final Logger LOG = LoggerFactory.getLogger(FieldScriptController.class);
    @Resource
    private RepairExplainService repairExplainService;
    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private CompanyModel companyModel;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private WorkOrderExplainModel workOrderExplainModel;

    /**
     * 更新负责人
     *
     * @param allScriptDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/updateOwnerOrCoUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateOwnerOrCoUser(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.updateOwnerOrCoUser(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/initNewAll", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initNewAll(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) throws XbbException {
        if (!proBaseConfig.getXbbCode().equals(allScriptDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.repairExplain(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/addCustomerPid", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addCustomerPid(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) throws XbbException {
        if (!proBaseConfig.getXbbCode().equals(allScriptDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.addCustomerPid(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除解释中三方数据id和三方数据来源
     *
     * @param allScriptDTO
     * @param br
     * @return
     * @throws XbbException
     */
    @RequestMapping(value = "/delCustomerPid", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delCustomerPid(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) throws XbbException {
        if (!proBaseConfig.getXbbCode().equals(allScriptDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.delCustomerPid(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/initNewAllProduct", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initNewAllProduct(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) throws XbbException {
        if (!proBaseConfig.getXbbCode().equals(allScriptDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.repairExplainProduct(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/changeExplainOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String changeExplainOrder(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) throws XbbException {
        if (!proBaseConfig.getXbbCode().equals(allScriptDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.changeExplainOrder(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/fixForm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fixForm(@RequestBody @Valid FixFormDTO fixFormDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.fixForm(fixFormDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/fixProductForm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fixProductForm(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.fixProductForm(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/fixProductFormAgain", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fixProductFormAgain(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws XbbException {
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
//        List<String> corpidList = paasFormExplainModel.getCorpidList();
        List<String> corpidList = Collections.singletonList("1");
//        corpidList.remove("0");
        Integer index = 1;
        for (String corpid : corpidList){
            LOG.warn("corpid:" + corpid + ":" + + index + "/" + corpidList.size());
            index ++;
            PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByBusinessType(XbbRefTypeEnum.PRODUCT.getCode(),corpid);
            if (Objects.isNull(paasFormExplainEntity)){
                continue;
            }
            List<FieldAttrEntity> fieldAttrEntityList = JSONArray.parseArray(paasFormExplainEntity.getExplains(),FieldAttrEntity.class);
            for (FieldAttrEntity fieldAttrEntity : fieldAttrEntityList){
                if (Objects.equals(ProductEnum.STOCK.getAttr(),fieldAttrEntity.getAttr())){
                    fieldAttrEntity.setEditable(0);
                    fieldAttrEntity.setShowEditable(0);
                }else if (Objects.equals(ProductEnum.SYSTEM_INFO.getAttr(),fieldAttrEntity.getAttr())){
                    fieldAttrEntity.setEditHide(1);
                    fieldAttrEntity.setShowType(ProductEnum.SYSTEM_INFO.getShowType());
                } else if (Objects.equals(ProductEnum.IS_NOTIFY.getAttr(),fieldAttrEntity.getAttr())){
                    fieldAttrEntity.setEditHide(1);
                    fieldAttrEntity.setShowType(ProductEnum.IS_NOTIFY.getShowType());
                }else if (Objects.equals(ProductEnum.STOCK_NOTIFY.getAttr(),fieldAttrEntity.getAttr())){
                    fieldAttrEntity.setEditHide(1);
                    fieldAttrEntity.setShowType(ProductEnum.STOCK_NOTIFY.getShowType());
                }else if (Objects.equals(ProductEnum.WAREHOUSE_INFO.getAttr(),fieldAttrEntity.getAttr())) {
                    fieldAttrEntity.setEditHide(1);
                    fieldAttrEntity.setShowType(ProductEnum.WAREHOUSE_INFO.getShowType());
                }else if (Objects.equals(ProductEnum.WAREHOUSE.getAttr(),fieldAttrEntity.getAttr())) {
                    fieldAttrEntity.setEditHide(1);
                    fieldAttrEntity.setShowType(ProductEnum.WAREHOUSE.getShowType());
                }
            }
            paasFormExplainEntity.setExplains(JSONArray.toJSONString(fieldAttrEntityList));
            paasFormExplainModel.update(paasFormExplainEntity);
        }
        return null;
    }

    @RequestMapping(value = "/selectProduct", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String selectProduct(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws Exception {
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        List<String> corpidList = fixDataJobDTO.getCorpidList();
        if (CollectionsUtil.isEmpty(corpidList)) {
            corpidList = companyModel.getCorpidList();
        }
        for (String corpid : corpidList) {
            PaasFormEntityExt productFormEntity = paasFormModel.getByBusinessType(XbbRefTypeEnum.PRODUCT.getCode(), corpid);
            List<Integer> businessTypeList;
            if (Objects.nonNull(fixDataJobDTO.getBusinessType())) {
                businessTypeList = Collections.singletonList(fixDataJobDTO.getBusinessType());
            } else {
                businessTypeList = Arrays.asList(201, 202, 301, 1001, 1101, 1201, 1301, 1404,1405,1406,1407,1408, 1504,1505,1506,1507,1508, 1601, 1701, 2301, 2601, 2801, 4700);
            }
            List<PaasFormExplainEntity> paasFormExplainEntityList = paasFormExplainModel.getListForUpdateProduct(corpid, businessTypeList);

            for (PaasFormExplainEntity paasFormExplainEntity : paasFormExplainEntityList) {
                Integer businessType = paasFormExplainEntity.getBusinessType();
                List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
                for (FieldAttrEntity entity : explainList) {
                    if (Objects.equals(FieldTypeEnum.SELECT_PRODUCT.getType(), entity.getFieldType())) {
                        PaasFormEntityExt paasFormEntityExt = new PaasFormEntityExt();
                        BeanUtil.copyProperties(productFormEntity, paasFormEntityExt);
                        InitExplainsHelper.setSelectProductField(entity, XbbRefTypeEnum.getByCode(businessType).getAlias(), paasFormEntityExt);
                    }
                }
                paasFormExplainEntity.setExplains(JSONArray.toJSONString(explainList));
                paasFormExplainModel.update(paasFormExplainEntity);
                LOG.warn("selectProduct shua expId:" + paasFormExplainEntity.getId());
            }
        }
        LOG.warn("selectProduct over");
        return null;
    }

    @RequestMapping(value = "/selectProductFixShowEditable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String selectProductFixShowEditable(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws Exception {
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        List<String> corpidList = fixDataJobDTO.getCorpidList();
        if (CollectionsUtil.isEmpty(corpidList)) {
            corpidList = companyModel.getCorpidList();
        }
        int count = 0;
        long start = DateTimeUtil.getInt();
        Map<String,Object> param;
        for (String corpid : corpidList) {
            List<Integer> businessTypeList;
            if (Objects.nonNull(fixDataJobDTO.getBusinessType())) {
                businessTypeList = Collections.singletonList(fixDataJobDTO.getBusinessType());
            } else {
                businessTypeList = Arrays.asList(201, 202, 301, 1001, 1101, 1201, 1301, 1404,1405,1406,1407,1408, 1504,1505,1506,1507,1508, 1601, 1701, 2601, 2801, 4700);
            }
            List<PaasFormExplainEntity> paasFormExplainEntityList = paasFormExplainModel.getListForUpdateProduct(corpid, businessTypeList);
            // 普通业务
            for (PaasFormExplainEntity paasFormExplainEntity : paasFormExplainEntityList) {
                List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
                for (FieldAttrEntity entity : explainList) {
                    if (Objects.equals(FieldTypeEnum.SELECT_PRODUCT.getType(), entity.getFieldType())) {
                        if (Objects.nonNull(entity.getSubForm()) && Objects.nonNull(entity.getSubForm().getItems())) {
                            List<? extends FieldAttrEntity> subFormExplainList = entity.getSubForm().getItems();
                            for (FieldAttrEntity subFieldAttrEntity : subFormExplainList) {
                                if (Objects.equals(subFieldAttrEntity.getAttr(), SelectProductEnum.PRODUCT.getAttr())) {
                                    subFieldAttrEntity.setShowEditable(1);
                                }
                            }
                            entity.getSubForm().setItems(subFormExplainList);
                        }
                    }
                }
                paasFormExplainEntity.setExplains(JSONArray.toJSONString(explainList));
                paasFormExplainModel.update(paasFormExplainEntity);
            }
            // 工单业务
            param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put(ParameterConstant.CORPID,corpid);
            param.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
            List<WorkOrderExplainEntity> workOrderExplainEntityList = workOrderExplainModel.list(param);
            for (WorkOrderExplainEntity paasFormExplainEntity : workOrderExplainEntityList) {
                List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
                for (FieldAttrEntity entity : explainList) {
                    if (Objects.equals(FieldTypeEnum.SELECT_PRODUCT.getType(), entity.getFieldType())) {
                        if (Objects.nonNull(entity.getSubForm()) && Objects.nonNull(entity.getSubForm().getItems())) {
                            List<? extends FieldAttrEntity> subFormExplainList = entity.getSubForm().getItems();
                            for (FieldAttrEntity subFieldAttrEntity : subFormExplainList) {
                                if (Objects.equals(subFieldAttrEntity.getAttr(), SelectProductEnum.PRODUCT.getAttr())) {
                                    subFieldAttrEntity.setShowEditable(1);
                                }
                            }
                            entity.getSubForm().setItems(subFormExplainList);
                        }
                    }
                }
                paasFormExplainEntity.setExplains(JSONArray.toJSONString(explainList));
                workOrderExplainModel.update(paasFormExplainEntity);
            }
            ++ count;
            LOG.warn("corpid" + corpid + "----------"+ count + "/" + corpidList.size());
        }
        LOG.warn("cosume" + (DateTimeUtil.getInt() - start));
        LOG.warn("success");
        return null;
    }

    /**
     * 刷工单产品amountFlag
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectWorkOrderProductFixAmountFlag", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String selectWorkOrderProductFixAmountFlag(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws Exception {
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        List<String> corpidList = fixDataJobDTO.getCorpidList();
        if (CollectionsUtil.isEmpty(corpidList)) {
            corpidList = companyModel.getCorpidList();
        }
        int count = 0;
        long start = DateTimeUtil.getInt();
        Map<String,Object> param;
        param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (String corpid : corpidList) {
            // 工单业务
            param.put(ParameterConstant.CORPID,corpid);
            param.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
            param.put("endAddTime", fixDataJobDTO.getAddTime());
            param.put("startAddTime", fixDataJobDTO.getAddTimeStart());
            List<WorkOrderExplainEntity> workOrderExplainEntityList = workOrderExplainModel.list(param);
            for (WorkOrderExplainEntity paasFormExplainEntity : workOrderExplainEntityList) {
                List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
                for (FieldAttrEntity entity : explainList) {
                    if (Objects.equals(FieldTypeEnum.SELECT_PRODUCT.getType(), entity.getFieldType())) {
                        if (Objects.nonNull(entity.getSubForm()) && Objects.nonNull(entity.getSubForm().getItems())) {
                            List<? extends FieldAttrEntity> subFormExplainList = entity.getSubForm().getItems();
                            for (FieldAttrEntity subFieldAttrEntity : subFormExplainList) {
                                if (Objects.equals(subFieldAttrEntity.getAttr(), SelectProductEnum.MEMO.getAttr())) {
                                    subFieldAttrEntity.setAmountFlag(0);
                                }
                            }
                            entity.getSubForm().setItems(subFormExplainList);
                        }
                    }
                }
                paasFormExplainEntity.setExplains(JSONArray.toJSONString(explainList));
                workOrderExplainModel.update(paasFormExplainEntity);
            }
            ++ count;
            LOG.warn("corpid" + corpid + "----------"+ count + "/" + corpidList.size());
        }
        LOG.warn("cosume" + (DateTimeUtil.getInt() - start));
        LOG.warn("success");
        return null;
    }

    @RequestMapping(value = "/selectWorkOrderProduct", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String selectWorkOrderProduct(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws Exception {
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        List<String> corpidList = fixDataJobDTO.getCorpidList();
        if (CollectionsUtil.isEmpty(corpidList)) {
            corpidList = companyModel.getCorpidList();
        }
        Integer count = 0;
        for (String corpid : corpidList){
            count ++;
            PaasFormEntityExt productFormEntity = paasFormModel.getByBusinessType(XbbRefTypeEnum.PRODUCT.getCode(), corpid);
            Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put("corpid",corpid);
            param.put("del",0);
            List<WorkOrderExplainEntity> paasFormExplainEntityList = workOrderExplainModel.list(param);
            for (WorkOrderExplainEntity paasFormExplainEntity : paasFormExplainEntityList) {
                List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
                for (FieldAttrEntity entity : explainList) {
                    if (Objects.equals(FieldTypeEnum.SELECT_PRODUCT.getType(), entity.getFieldType())) {
                        PaasFormEntityExt paasFormEntityExt = new PaasFormEntityExt();
                        BeanUtil.copyProperties(productFormEntity, paasFormEntityExt);
                        InitExplainsHelper.setSelectWorkOrderProductField(entity, productFormEntity);
                    }
                }
                paasFormExplainEntity.setExplains(JSONArray.toJSONString(explainList));
                workOrderExplainModel.update(paasFormExplainEntity);
            }
            LOG.warn("公司" + count + "/" + corpidList.size());
        }
        LOG.warn("selectWorkOrderProduct over");
        return null;
    }

    @RequestMapping(value = "/productTotalFormula", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String productTotalFormula(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws Exception {
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        List<String> corpidList = fixDataJobDTO.getCorpidList();
        if (CollectionsUtil.isEmpty(corpidList)) {
            corpidList = companyModel.getCorpidList();
        }
        Integer count = 0;
        for (String corpid : corpidList) {
            count ++;
            List<PaasFormExplainEntity> listForUpdateProduct = paasFormExplainModel.getListForUpdateProduct(corpid, Arrays.asList(XbbRefTypeEnum.CONTRACT.getCode(), XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), XbbRefTypeEnum.PURCHASE.getCode(), XbbRefTypeEnum.REFUND.getCode()));
            for (PaasFormExplainEntity paasFormExplainEntity : listForUpdateProduct) {
                Integer businessType = paasFormExplainEntity.getBusinessType();
                List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
                XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);
                switch (xbbRefTypeEnum) {
                    case CONTRACT:
                        for (FieldAttrEntity fieldAttrEntity : explainList) {
                            if (Objects.equals(fieldAttrEntity.getAttr(), ContractEnum.PRODUCT_TOTAL.getAttr())) {
                                fieldAttrEntity.setEditable(0);
                                DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
                                FormulaPoJo formulaInfo = new FormulaPoJo();
                                String formula = "SUM({self." + ContractEnum.PRODUCT.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr() + "})";
                                JSONObject labelMap = new JSONObject();
                                labelMap.put("self." + ContractEnum.PRODUCT.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr(), "关联产品.售价小计");
                                formulaInfo.setFormula(formula);
                                formulaInfo.setLabelMap(labelMap);
                                defaultAttrPoJo.setDefaultType("formula");
                                defaultAttrPoJo.setFormulaInfo(formulaInfo);
                                fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
                                // 合同金额默认限定 0 ~ 100000000000
                                NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
                                numericalLimits.setMin(0D);
                                numericalLimits.setMax(100000000000D);
                                fieldAttrEntity.setNumericalLimits(numericalLimits);
                                fieldAttrEntity.setNumericalLimitsFlag(1);
                            }
                        }
                        break;
                    case PURCHASE:
                        for (FieldAttrEntity fieldAttrEntity : explainList) {
                            if (Objects.equals(fieldAttrEntity.getAttr(), PurchaseEnum.PRODUCT_TOTAL.getAttr())) {
                                fieldAttrEntity.setEditable(0);
                                DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
                                FormulaPoJo formulaInfo = new FormulaPoJo();
                                String formula = "SUM({self." + PurchaseEnum.PRODUCT.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr() + "})";
                                JSONObject labelMap = new JSONObject();
                                labelMap.put("self." + PurchaseEnum.PRODUCT.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr(), "关联产品.单价小计");
                                formulaInfo.setFormula(formula);
                                formulaInfo.setLabelMap(labelMap);
                                defaultAttrPoJo.setDefaultType("formula");
                                defaultAttrPoJo.setFormulaInfo(formulaInfo);
                                fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
                                // 合同金额默认限定 0 ~ 100000000000
                                NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
                                numericalLimits.setMin(0D);
                                numericalLimits.setMax(100000000000D);
                                fieldAttrEntity.setNumericalLimits(numericalLimits);
                                fieldAttrEntity.setNumericalLimitsFlag(1);
                            }
                        }
                        break;
                    case SALES_OPPORTUNITY:
                        for (FieldAttrEntity fieldAttrEntity : explainList) {
                            if (Objects.equals(fieldAttrEntity.getAttr(), SalesOpportunityEnum.ESTIMATE_AMOUNT.getAttr())) {
                                fieldAttrEntity.setEditable(0);
                                DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
                                FormulaPoJo formulaInfo = new FormulaPoJo();
                                String formula = "SUM({self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr() + "})";
                                JSONObject labelMap = new JSONObject();
                                labelMap.put("self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr(), "关联产品.售价小计");
                                formulaInfo.setFormula(formula);
                                formulaInfo.setLabelMap(labelMap);
                                defaultAttrPoJo.setDefaultType("formula");
                                defaultAttrPoJo.setFormulaInfo(formulaInfo);
                                fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
                                // 合同金额默认限定 0 ~ 100000000000
                                NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
                                numericalLimits.setMin(0D);
                                numericalLimits.setMax(100000000000D);
                                fieldAttrEntity.setNumericalLimits(numericalLimits);
                                fieldAttrEntity.setNumericalLimitsFlag(1);
                            }
                        }
                        break;
                    case REFUND:
                        for (FieldAttrEntity fieldAttrEntity : explainList) {
                            if (Objects.equals(fieldAttrEntity.getAttr(), RefundEnum.RETURN_AMOUNT.getAttr())) {
                                fieldAttrEntity.setEditable(0);
                                DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
                                FormulaPoJo formulaInfo = new FormulaPoJo();
                                String formula = "SUM({self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr() + "})";
                                JSONObject labelMap = new JSONObject();
                                labelMap.put("self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr(), "关联产品.单价小计");
                                formulaInfo.setFormula(formula);
                                formulaInfo.setLabelMap(labelMap);
                                defaultAttrPoJo.setDefaultType("formula");
                                defaultAttrPoJo.setFormulaInfo(formulaInfo);
                                fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
                                // 合同金额默认限定 0 ~ 100000000000
                                NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
                                numericalLimits.setMin(0D);
                                numericalLimits.setMax(100000000000D);
                                fieldAttrEntity.setNumericalLimits(numericalLimits);
                                fieldAttrEntity.setNumericalLimitsFlag(1);
                            }
                        }
                        break;
                    default:
                        break;
                }
                paasFormExplainEntity.setExplains(JSONArray.toJSONString(explainList));
                paasFormExplainModel.update(paasFormExplainEntity);
            }
            LOG.warn("公司" + count + "/" + corpidList.size());
        }
        LOG.warn("success");
        return null;
    }

    @RequestMapping(value = "/fixReturnedPurchaseForm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fixReturnedPurchaseForm(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws XbbException {
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        long start = DateTimeUtil.getInt();
        List<String> corpidList = paasFormExplainModel.getCorpidList();
//        List<String> corpidList = Collections.singletonList("1");
//        corpidList.remove("0");
        List<String> enumAttrs = InitExplainsHelper.getBusinessEnumAttrs(ReturnedPurchaseEnum.class);
        FieldAttrEntity supplier = InitExplainsHelper.setField(ReturnedPurchaseEnum.class, enumAttrs, ReturnedPurchaseEnum.LINK_SUPPLIER);
        TempReturnedPurchaseEnum.initLinkSupplier(supplier);
        FieldAttrEntity supplierText = InitExplainsHelper.setField(ReturnedPurchaseEnum.class, enumAttrs, ReturnedPurchaseEnum.LINK_SUPPLIER_HIDE);
        Integer index = 1;
        for (String corpid : corpidList){
            LOG.warn("corpid:" + corpid + ":" + + index + "/" + corpidList.size());
            index ++;
            PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByBusinessType(XbbRefTypeEnum.RETURNED_PURCHASE.getCode(),corpid);
            if (Objects.isNull(paasFormExplainEntity)){
                continue;
            }
            List<FieldAttrEntity> fieldAttrEntityList = JSONArray.parseArray(paasFormExplainEntity.getExplains(),FieldAttrEntity.class);
            int sheetNoIndex = 0;
            int expIndex = 0;
            boolean hasSupplier = false;
            for (FieldAttrEntity fieldAttrEntity : fieldAttrEntityList){
                if (Objects.equals(fieldAttrEntity.getAttr(),ReturnedPurchaseEnum.SHEET_NO.getAttr())){
                    sheetNoIndex = expIndex;
                }else if (Objects.equals(fieldAttrEntity.getAttr(),ReturnedPurchaseEnum.LINK_SUPPLIER.getAttr())){
                    hasSupplier = true;
                }else if (Objects.equals(fieldAttrEntity.getAttr(),ReturnedPurchaseEnum.PURCHASE_SHEET_ID.getAttr())){
                    fieldAttrEntity.getSaasParticularAttributePoJo().setRelyAttr(ReturnedPurchaseEnum.LINK_SUPPLIER.getAttr());
                    fieldAttrEntity.getSaasParticularAttributePoJo().setRelyBusiness(XbbRefTypeEnum.SUPPLIER.getCode());
                }
                expIndex++;
            }
            if (!hasSupplier){
                fieldAttrEntityList.add(sheetNoIndex,supplier);
                fieldAttrEntityList.add(supplierText);
                paasFormExplainEntity.setExplains(JSONArray.toJSONString(fieldAttrEntityList));
                paasFormExplainModel.update(paasFormExplainEntity);
            }
        }
        LOG.warn("consume:" + (DateTimeUtil.getInt() - start));
        LOG.warn("success");
        return null;
    }

    @RequestMapping(value = "/fixShowAttrPermission", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fixShowAttrPermission(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.fixShowAttrPermission(fixDataJobDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/fixContractProductSellPriceFormula", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fixContractProductSellPriceFormula(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.fixContractProductSellPriceFormula(fixDataJobDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/fixQuotationProductSellPriceFormula", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fixQuotationProductSellPriceFormula(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.fixQuotationProductSellPriceFormula(fixDataJobDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/addCustomerExplain", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addCustomerExplain(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.addCustomerExplain(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/fixProductUnit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fixProductUnit(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.fixProductUnit(fixDataJobDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /***
     * 刷工商查询字段：销客对应精线索
     *
     * @param fieldSetDTO
     * @param: br
     * @throws
     * @return java.lang.String
     * @author hongxiao
     * @date 2021-12-26 17:03
     * @since
     * @version
     */
    @RequestMapping(value = "/fineClueMatchSearch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fineClueMatchSearch(@RequestBody @Valid FieldSetDTO fieldSetDTO, BindingResult br) throws XbbException{
        if (!proBaseConfig.getXbbCode().equals(fieldSetDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.fineClueMatchSearch(fieldSetDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * @Author mingliang
     * @Description 阶段推进器所需脚本，给客户，机会，线索表单添加long_1,long_2,long_3,阶段字段类型修改为阶段推进器
     * @Date 2021/12/30 14:44
     * @param allScriptDTO
     * @param br
     **/
    @RequestMapping(value = "/addCustomerStage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addCustomerStage(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.addCustomerStage(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 公海池需求
     * 客户新增字段：BACK_NUM、PUBLIC_GROUP、CONTRACT_SIGN_DATE、OPPORTUNITY_ADD_DATE
     * 线索新增字段：BACK_NUM、PUBLIC_GROUP
     * @author lijiangren
     * @date 2022-04-12 18 58
     */
    @RequestMapping(value = "/addPublicPoolField", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addPublicPoolField(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.addPublicPoolField(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 公海池需求（回滚SQL）
     * 客户新增字段：BACK_NUM、PUBLIC_GROUP、CONTRACT_SIGN_DATE、OPPORTUNITY_ADD_DATE
     * 线索新增字段：BACK_NUM、PUBLIC_GROUP
     * @author lijiangren
     * @date 2022-04-12 18 58
     */
    @RequestMapping(value = "/removePublicPoolField", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String removePublicPoolField(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.removePublicPoolField(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 机会新增竞争对手相关字段：关联竞争对手，赢单竞品
     * date: 2022/8/15 9:12 上午
     * @author yingjf
     * @param allScriptDTO:
     * @param br:
     */
    @RequestMapping(value = "/addCompetitor", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addCompetitor(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.addCompetitor(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/removeCompetitor", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String removeCompetitor(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.removeCompetitor(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
